package com.lm.eec.controller;

import com.lm.eec.dto.UserExportDTO;
import com.lm.eec.example.UserExcelExport;
import com.lm.eec.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
public class EECController {

    @Autowired
    private UserService userService;

    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=users.xlsx");

        List<UserExportDTO> dataList = userService.listAll();
        new UserExcelExport().export(dataList, response.getOutputStream());
    }
}
